<template>
  <div class="post-detail-page">
    <Modal
      title="删除文章"
      :visible="modalVisible"
      @modal-on-close="modalVisible = false"
      @modal-on-confirm="hideAndDelete"
    >
      <p>是否确认要删除这篇文章?</p>
    </Modal>
    <article class="w-75 mx-auto mb-5 pb-3" v-if="details">
      <img :src="currentImageUrl" alt="currentPost.title" v-if="currentImageUrl" class="rounded-lg img-fluid my-4">
      <h2 class="mb-4">{{details.title}}</h2>
      <div class="user-profile-component border-top border-bottom py-3 mb-5 align-items-center row g-0">
        <div class="col">
          <user-profile :user="details.author"></user-profile>
        </div>
        <span class="text-muted col text-right font-italic">发表于：{{details.createdAt}}</span>
      </div>
      <div v-html="currentHTML"></div>
      <div v-if="showEditArea" class="btn-group mt-5">
        <router-link
          type="button"
          class="btn btn-success"
          :to="{ name: 'create', query: { id: details._id } }"
        >
          编辑
        </router-link>
        <button type="button" style="margin-left: 10px;" class="btn btn-danger" @click="modalVisible = true">删除</button>
      </div>
    </article>
  </div>
</template>

<script lang="ts">
import { defineComponent, onMounted, computed, ref } from 'vue'
import { useStore } from 'vuex'
import { useRoute, useRouter } from 'vue-router'
import { ImageProps, GlobalDataProps, UserProps } from './../store'
import MarkdownIt from 'markdown-it'
import UserProfile from './../components/UserProfile.vue'
import Modal from './../components/Modal.vue'
import createMessage from './../components/CreateMessage'
export default defineComponent({
  name: 'post-details',
  components: {
    UserProfile,
    Modal
  },
  setup () {
    const store = useStore<GlobalDataProps>()
    const route = useRoute()
    const router = useRouter()
    const modalVisible = ref(false)
    const id = route.params.id
    const md = new MarkdownIt()
    onMounted(() => {
      store.dispatch('fetchPost', id)
    })
    const details = computed(() => store.getters.getPostDetailById(id))
    const currentImageUrl = computed(() => {
      if (details.value && details.value.image) {
        const { image } = details.value
        return (image as ImageProps).url + '?x-oss-process=image/resize,w_850'
      } else {
        return null
      }
    })
    const showEditArea = computed(() => {
      const { isLogin, _id } = store.state.user
      if (details.value && details.value.author && isLogin) {
        const postAuthor = details.value.author as UserProps
        return postAuthor._id === _id
      } else {
        return false
      }
    })
    const currentHTML = computed(() => {
      const { content, isHTML } = details.value
      if (details.value && content) {
        return isHTML ? content : md.render(content)
      } else {
        return ''
      }
    })
    const hideAndDelete = () => {
      modalVisible.value = false
      store.dispatch('deletePost', id).then(() => {
        createMessage('删除成功，两秒后跳转到专栏', 'success', 2000)
        setTimeout(() => {
          router.push({ name: 'column', params: { id: store.state.user.column } })
        }, 2000)
      })
    }
    return {
      details,
      currentImageUrl,
      currentHTML,
      showEditArea,
      modalVisible,
      hideAndDelete
    }
  }
})
</script>
